package com.soar.recruitment.module.info.bll.controller;


import android.text.TextUtils;

import com.soar.framework.net.http.OkHttpUtils;
import com.soar.recruitment.common.bll.LoginManager;
import com.soar.recruitment.common.bll.SOARHttpCallback;
import com.soar.recruitment.common.dal.model.SOARBaseModel;
import com.soar.recruitment.common.util.MyToast;
import com.soar.recruitment.module.user.bll.api.UserApi;
import com.soar.recruitment.module.user.bll.controller.ForgetContrats;
import com.soar.recruitment.module.user.bll.controller.LoginContrats;
import com.soar.recruitment.module.user.dal.bean.UserModel;

import java.util.HashMap;
import java.util.Map;

public class Inforesenter implements InfoContrats.Presenter {

    // Http - tag
    private final static String TAG_LOGIN = "login";

    private InfoContrats.View mView;
    private UserApi mUserApi;

    public Inforesenter(InfoContrats.View view) {
        this.mView = view;
        this.mUserApi = new UserApi();
    }

    @Override
    public void signLogin() {
        String member_email = mView.getPhoneNum();
        if(TextUtils.isEmpty(member_email)) {
            MyToast.show("手机号码不能为空");
            return;
        }
        String member_password = mView.getPwdNum();
        if(TextUtils.isEmpty(member_password)) {
            MyToast.show("密码不能为空");
            return;
        }

        String captcha = "";

        Map<String, String> params = new HashMap<>();
        params.put("member_email", member_email);//member_phone
        params.put("member_password", member_password);
        params.put("captcha", captcha);
        params.put("login_type", "mobile");

        mUserApi.login(TAG_LOGIN, params, new SOARHttpCallback<SOARBaseModel<UserModel>>() {
            @Override
            public void onRequestSuccess(Object response) {
                UserModel userModel = (UserModel) response;
                if (userModel != null) {
                    LoginManager.loginSuccess(userModel);
                    mView.goNext();
                } else {
                    MyToast.show("数据错误~");
                }
            }
        });
    }

    @Override
    public void destory() {
        OkHttpUtils.getInstance().cancelTag(TAG_LOGIN);
    }

}
